package com.youkeyi.ddy.cloud.basic.crm.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.customer.entity.SysCustomer;
import com.youkeyi.ddy.cloud.basic.crm.order.entity.SysCustomerOrder;
import com.youkeyi.ddy.cloud.basic.crm.order.service.SysCustomerOrderService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.CrmCustomerOrderCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.dto.CrmCustomerOrderListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.dto.CrmCustomerOrderListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerOrderCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerOrderDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerOrderQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerOrderUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户订单信息 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-03-04
 */
@Service
public class SysCustomerOrderController implements CrmCustomerOrderCloudService {
    
    @Autowired
    private SysCustomerOrderService sysCustomerOrderService;

    @Override
    public CrmCustomerOrderListCloudDto query(CrmCustomerOrderQueryCloudParam param) {
        CrmCustomerOrderListCloudDto dto = new CrmCustomerOrderListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getCustomerOrderPrimaryKeys())) {
            param.getCustomerOrderPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getFinanceCorpKeys())) {
            param.getFinanceCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCustomerKeys())) {
            param.getCustomerKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getStatuses())) {
            param.getStatuses().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getPublicUserKeys())) {
            param.getPublicUserKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysCustomerOrder> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getCustomerOrderPrimaryKeys()), SysCustomerOrder::getPrimaryKey, param.getCustomerOrderPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysCustomerOrder::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getFinanceCorpKeys()), SysCustomerOrder::getFinanceCorpKey, param.getFinanceCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCustomerKeys()), SysCustomerOrder::getCustomerKey, param.getCustomerKeys())
                .in(CollectionUtils.isNotEmpty(param.getStatuses()), SysCustomerOrder::getStatus, param.getStatuses())
                .eq(StringUtils.isNotBlank(param.getSourceDepartmentKey()), SysCustomerOrder::getSourceDepartmentKey, param.getSourceDepartmentKey())
                .eq(StringUtils.isNotBlank(param.getSourceUserKey()), SysCustomerOrder::getSourceUserKey, param.getSourceUserKey())
                .eq(StringUtils.isNotBlank(param.getServiceSectionKey()), SysCustomerOrder::getServiceSectionKey, param.getServiceSectionKey())
                .eq(StringUtils.isNotBlank(param.getServiceUserKey()), SysCustomerOrder::getServiceUserKey, param.getServiceUserKey())
                .like(StringUtils.isNotBlank(param.getCustomerNameFuzzy()), SysCustomerOrder::getCustomerName, param.getCustomerNameFuzzy())
                .like(StringUtils.isNotBlank(param.getPotentialDemandKey()), SysCustomerOrder::getPotentialDemandKey, param.getPotentialDemandKey())
                .between(null != param.getRegistrationTimeBegin() && null != param.getRegistrationTimeEnd(), SysCustomerOrder::getRegistrationTime, param.getRegistrationTimeBegin(), param.getRegistrationTimeEnd())
                .between(null != param.getCompletionTimeBegin() && null != param.getCompletionTimeEnd(), SysCustomerOrder::getCompletionTime, param.getCompletionTimeBegin(), param.getCompletionTimeEnd())
                .ne(SysCustomerOrder::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;
        if (CollectionUtils.isNotEmpty(param.getPublicUserKeys())) {
            wrapper.lambda().and( i -> i
                    .in(SysCustomerOrder::getServiceUserKey, param.getPublicUserKeys())
                    .or().in(SysCustomerOrder::getSourceUserKey, param.getPublicUserKeys())
            );
        }
        wrapper.lambda().orderByDesc(SysCustomerOrder::getId);

        Page<SysCustomerOrder> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysCustomerOrder> customerOrderList = sysCustomerOrderService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(customerOrderList.getRecords())) {
            dto.setTotal(customerOrderList.getTotal());

            List<CrmCustomerOrderListItemCloudDto> items = new ArrayList<>();
            customerOrderList.getRecords().forEach(customerOrder -> {
                CrmCustomerOrderListItemCloudDto item = new CrmCustomerOrderListItemCloudDto();
                item.setCustomerOrderPrimaryKey(customerOrder.getPrimaryKey());
                item.setCorpKey(customerOrder.getCorpKey());
                item.setFinanceCorpKey(customerOrder.getFinanceCorpKey());
                item.setCustomerKey(customerOrder.getCustomerKey());
                item.setCustomerName(customerOrder.getCustomerName());
                item.setServiceSectionKey(customerOrder.getServiceSectionKey());
                item.setServiceSectionName(customerOrder.getServiceSectionName());
                item.setServiceItemsKey(customerOrder.getServiceItemsKey());
                item.setServiceItemsName(customerOrder.getServiceItemsName());
                item.setServiceUserKey(customerOrder.getServiceUserKey());
                item.setServiceUserName(customerOrder.getServiceUserName());
                item.setIncomeRmb(customerOrder.getIncomeRmb());
                item.setIncomeHkd(customerOrder.getIncomeHkd());
                item.setAnnexKey(customerOrder.getAnnexKey());
                item.setRemark(customerOrder.getRemark());
                item.setSourceDepartmentKey(customerOrder.getSourceDepartmentKey());
                item.setSourceDepartmentName(customerOrder.getSourceDepartmentName());
                item.setSourceUserName(customerOrder.getSourceUserName());
                item.setSourceUserKey(customerOrder.getSourceUserKey());
                item.setRegistrationTime(customerOrder.getRegistrationTime() != null ? customerOrder.getRegistrationTime().getTime() : null);
                item.setCompletionTime(customerOrder.getCompletionTime() != null ? customerOrder.getCompletionTime().getTime() : null);
                item.setPotentialDemandKey(customerOrder.getPotentialDemandKey());
                item.setPotentialDemandName(customerOrder.getPotentialDemandName());
                item.setStatus(customerOrder.getStatus());
                item.setIsDeleted(customerOrder.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(customerOrder.getCreateTime() != null ? customerOrder.getCreateTime().getTime() : null);
                item.setCreateUserKey(customerOrder.getCreateUserKey());
                item.setCreateUserName(customerOrder.getCreateUserName());
                item.setUpdateTime(customerOrder.getUpdateTime() != null ? customerOrder.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(customerOrder.getUpdateUserKey());
                item.setUpdateUserName(customerOrder.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmCustomerOrderCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getCustomerKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysCustomerOrder customerOrder = new SysCustomerOrder();
        customerOrder.setPrimaryKey(SimpleUUidUtils.getUuid());
        customerOrder.setCorpKey(param.getCorpKey());
        customerOrder.setFinanceCorpKey(param.getFinanceCorpKey());
        customerOrder.setCustomerKey(param.getCustomerKey());
        customerOrder.setCustomerName(param.getCustomerName());
        customerOrder.setServiceSectionKey(param.getServiceSectionKey());
        customerOrder.setServiceSectionName(param.getServiceSectionName());
        customerOrder.setServiceItemsKey(param.getServiceItemsKey());
        customerOrder.setServiceItemsName(param.getServiceItemsName());
        customerOrder.setServiceUserKey(param.getServiceUserKey());
        customerOrder.setServiceUserName(param.getServiceUserName());
        customerOrder.setIncomeRmb(param.getIncomeRmb());
        customerOrder.setIncomeHkd(param.getIncomeHkd());
        customerOrder.setAnnexKey(param.getAnnexKey());
        customerOrder.setRemark(param.getRemark());
        customerOrder.setSourceDepartmentKey(param.getSourceDepartmentKey());
        customerOrder.setSourceDepartmentName(param.getSourceDepartmentName());
        customerOrder.setSourceUserName(param.getSourceUserName());
        customerOrder.setSourceUserKey(param.getSourceUserKey());
        customerOrder.setCompletionTime(param.getCompletionTime() != null ? new Date(param.getCompletionTime()) : null);
        customerOrder.setRegistrationTime(param.getRegistrationTime() != null ? new Date(param.getRegistrationTime()) : null);
        customerOrder.setPotentialDemandKey(param.getPotentialDemandKey());
        customerOrder.setPotentialDemandName(param.getPotentialDemandName());
        customerOrder.setStatus(param.getStatus());
        customerOrder.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        customerOrder.setCreateUserKey(param.getCreateUserKey());
        customerOrder.setCreateUserName(param.getCreateUserName());
        customerOrder.setCreateTime(new Date());
        boolean save = sysCustomerOrderService.save(customerOrder);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(customerOrder.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmCustomerOrderUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCustomerOrderPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysCustomerOrder> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysCustomerOrder::getPrimaryKey, param.getCustomerOrderPrimaryKey());
        SysCustomerOrder customerOrder = new SysCustomerOrder();
        customerOrder.setPrimaryKey(param.getCustomerOrderPrimaryKey());
        customerOrder.setFinanceCorpKey(param.getFinanceCorpKey());
        customerOrder.setCustomerKey(param.getCustomerKey());
        customerOrder.setCustomerName(param.getCustomerName());
        customerOrder.setServiceSectionKey(param.getServiceSectionKey());
        customerOrder.setServiceSectionName(param.getServiceSectionName());
        customerOrder.setServiceItemsKey(param.getServiceItemsKey());
        customerOrder.setServiceItemsName(param.getServiceItemsName());
        customerOrder.setServiceUserKey(param.getServiceUserKey());
        customerOrder.setServiceUserName(param.getServiceUserName());
        customerOrder.setIncomeRmb(param.getIncomeRmb());
        customerOrder.setIncomeHkd(param.getIncomeHkd());
        customerOrder.setAnnexKey(param.getAnnexKey());
        customerOrder.setRemark(param.getRemark());
        customerOrder.setSourceDepartmentKey(param.getSourceDepartmentKey());
        customerOrder.setSourceDepartmentName(param.getSourceDepartmentName());
        customerOrder.setSourceUserName(param.getSourceUserName());
        customerOrder.setSourceUserKey(param.getSourceUserKey());
        customerOrder.setCompletionTime(param.getCompletionTime() != null ? new Date(param.getCompletionTime()) : null);
        customerOrder.setRegistrationTime(param.getRegistrationTime() != null ? new Date(param.getRegistrationTime()) : null);
        customerOrder.setPotentialDemandKey(param.getPotentialDemandKey());
        customerOrder.setPotentialDemandName(param.getPotentialDemandName());
        customerOrder.setStatus(param.getStatus());
        customerOrder.setUpdateUserKey(param.getUpdateUserKey());
        customerOrder.setUpdateUserName(param.getUpdateUserName());
        customerOrder.setUpdateTime(new Date());
        boolean save = sysCustomerOrderService.update(customerOrder, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmCustomerOrderDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getCustomerOrderPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysCustomerOrder> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysCustomerOrder::getPrimaryKey, param.getCustomerOrderPrimaryKeys());

        SysCustomerOrder customerOrder = new SysCustomerOrder();
        customerOrder.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        customerOrder.setUpdateUserKey(param.getUpdateUserKey());
        customerOrder.setUpdateUserName(param.getUpdateUserName());
        customerOrder.setUpdateTime(new Date());
        boolean save = sysCustomerOrderService.update(customerOrder, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
